package com.prodog.gamemodule.fight.effect.manager;

import cn.hutool.core.util.ReflectUtil;
import com.prodog.gamemodule.fight.effect.entity.FightEffect;
import com.prodog.gamemodule.fight.effect.handler.EffectHandler;
import com.prodog.gamemodule.fight.effect.handler.impl.ParalysisEffectHandler;
import com.prodog.gamemodule.fight.effect.handler.impl.ThunderEffectHandler;
import com.prodog.gamemodule.fight.fightobj.entity.FightObj;
import com.prodog.gamemodule.fight.session.FightSession;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

public class EffectHandlerManager {
    public static Map<String, Constructor<? extends EffectHandler>> handlerMap;

    static {
        handlerMap = new HashMap() {{
            put("test", ReflectUtil.getConstructor(ThunderEffectHandler.class, FightSession.class, FightEffect.class));
            put("paralysis", ReflectUtil.getConstructor(ParalysisEffectHandler.class, FightSession.class, FightEffect.class));
        }};
    }

    public static void onFightStart(FightSession session, FightEffect effect, FightObj obj) {
        try {
            EffectHandler handler = handlerMap.get(effect.getName()).newInstance(session, effect);
            handler.onFightStart(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void beforeAction(FightSession session, FightEffect effect, FightObj obj) {
        try {
            EffectHandler handler = handlerMap.get(effect.getName()).newInstance(session, effect);
            handler.beforeAction(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void afterExecuteDamage(FightSession session, FightEffect effect, FightObj obj, FightObj beActedObj) {
        try {
            EffectHandler handler = handlerMap.get(effect.getName()).newInstance(session, effect);
            handler.afterExecuteDamage(obj,beActedObj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void roundDown(FightSession session, FightEffect effect, FightObj obj) {
        if (effect.getRound() != null) {
            effect.setRound(effect.getRound() - 1);
            if (effect.getRound() == 0) {
                obj.getEffects().remove(effect);
            }
        }
    }

    public static void afterBeExecutedDamage(FightSession session, FightEffect effect, FightObj actObj, FightObj beActedObj) {
        try {
            EffectHandler handler = handlerMap.get(effect.getName()).newInstance(session, effect);
            handler.afterBeExecutedDamage(actObj,beActedObj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
